
package c.x.jy.wexin.controller;


import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import c.x.jy.common.annotation.Login;
import c.x.jy.common.utils.R;
import c.x.jy.common.utils.SMSUtil;
import c.x.jy.common.utils.SmsConfig;
import c.x.jy.common.utils.SysUtils;
import c.x.jy.dao.entity.MemberEntity;
import c.x.jy.dao.entity.MemberWalletEntity;
import c.x.jy.service.config.WechatConfigService;
import c.x.jy.service.redis.Cache;
import c.x.jy.service.service.BusinessService;
import c.x.jy.service.service.CaptchaService;
import c.x.jy.service.service.MemberService;
import c.x.jy.service.service.MemberWalletService;
import c.x.jy.service.utils.MemberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 登录授权
 *
 */
@Slf4j
@Controller
@RequestMapping("/wechat")
@Api("APP接口")
public class LoginController {
	
    @Autowired
    private   MemberService customerService;
    @Autowired
    private  WechatConfigService wechatConfigService;
    @Autowired
    private CaptchaService captchaService;
	@Autowired
	private Cache redisUtils;
	@Autowired
	private Environment env;

	@Autowired
	private BusinessService businessService;
	@Autowired
	private MemberWalletService memberWalletService;

	@Login
	@Transactional
	@ResponseBody
    @PostMapping("member/build")
    @ApiOperation("绑定用户")
    public R saveCustomer(@RequestBody MemberEntity customer){
		String code=	redisUtils.get(customer.getPhone());
		 
    	if(customer.getCode()==null||!customer.getCode().equals(code)) {
    		 return R.error("绑定失败！");
    	}
    	 customer.setOpenid(MemberUtil.get().getOpenid());
    	 MemberEntity m=new MemberEntity();
    	 m.setOpenid(MemberUtil.get().getOpenid());
    	 m.setPhone(customer.getPhone());
    	 m.setReferralCode(SysUtils.generateShortCode());
    	if(!StringUtils.isEmpty( customer.getReferralCode())) {
    		MemberEntity pareUser=	customerService.selectByReferralCode(customer.getReferralCode());
    		if(pareUser!=null)
        	m.setParentId(pareUser.getId());
    	}

		m=  customerService.saveOrUpdateByOpenid(m);
		  MemberWalletEntity mw=new MemberWalletEntity();
		  mw.setMemberId(m.getId());
		  memberWalletService.save(mw);
		  return R.put(m) ;
    }
 
	@Login
	@ResponseBody
    @GetMapping("userInfo")
    public R userInfo( ) {
		
 
		return R.put(customerService.getCurrUser());
	}
	
    
	/**
	 * 验证码
	 */
	@GetMapping("captcha.jpg")
	public void captcha(HttpServletResponse response, String uuid) throws IOException {
		response.setHeader("Cache-Control", "no-store, no-cache");
		response.setContentType("image/jpeg");

		// 获取图片验证码
		BufferedImage image = captchaService.getCaptcha(uuid);

		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(image, "jpg", out);
		IOUtils.closeQuietly(out);
	}
	
	@Login
	@GetMapping("tologin")
	public String tologin(HttpServletResponse response, HttpServletRequest request) throws IOException {
		 String contextPath = ((HttpServletRequest) request).getContextPath();
			if(!StringUtils.isEmpty(contextPath))contextPath="/"+contextPath;
		MemberEntity member=	customerService.getCurrUser();
 
  
//		if(StringUtils.isEmpty(member.getPhone()))return "redirect:/index.html#!wx_login";
		return "redirect:/index.html#!page/home/home";
		
	}
	
	@Login
	@GetMapping("topage")
	public String topage(HttpServletResponse response, HttpServletRequest request,String page)   {
		MemberEntity member=	customerService.getCurrUser();	
//		if(StringUtils.isEmpty(member.getPhone()))return "redirect:/index.html#!wx_login";
		return "redirect:/index.html#!"+page;
	}

	@Login
	@ResponseBody
	@GetMapping("sms")
	public R sendsms(String uuid ,String phone) throws IOException {
 
	String code=	redisUtils.get(phone); 
 
	if(code==null) {
	 SmsConfig smsConfig= wechatConfigService.getDefultSmsConfing();
       code=	 SMSUtil.sendByRegister(phone, smsConfig);	
       if(code==null) return R.error("获取验证码失败！");
       redisUtils.set(  phone, code, 10*60l);
       return R.ok();
	} else
	
	return R.error("已经发送过了,请不要重复 发送！");
	}
 
 

}
